<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :inline="true" size="small" :model="listQuery.params" class="filter-form">
      <div>
        <el-form-item label="姓名">
          <el-input v-model="listQuery.params.realName" placeholder="请输入姓名" width="100"/>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="listQuery.params.phone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="listQuery.params.idCard" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="单位名称">
          <el-input v-model="listQuery.params.companyName" placeholder="请输入单位名称"/>
        </el-form-item>
        <el-form-item label="是否有效">
          <el-select v-model="listQuery.params.effect" placeholder="请选择" style="width: 120px;">
            <el-option label="全部" value=""/>
            <el-option label="有效" :value="true"/>
            <el-option label="无效" :value="false"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="fetchApplyList">查询</el-button>
        </el-form-item>
      </div>
      <div>
        <el-form-item>
          <el-button type="primary" @click="exportExamResult">导出试卷</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="exportQRCode">导出二维码</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" @click="exportExcel">导出表格</el-button>
        </el-form-item>
      </div>
    </el-form>

    <!-- 表格列表 -->
    <el-table :data="tableData" v-loading="loading" border @selection-change="handleSelectionChange"
              style="width: 100%">
      <el-table-column
        type="selection"
        width="40">
      </el-table-column>
      <el-table-column prop="realName" label="姓名" width="100"/>
      <el-table-column prop="phone" label="手机号" width="140"/>
      <el-table-column label="个人照片" width="120">
        <template v-slot="scope">
          <el-image :src="scope.row.photoUrl" style="width: 60px; height: 60px;" fit="cover"/>
        </template>
      </el-table-column>
      <el-table-column prop="idCard" label="身份证号" width="180"/>
      <el-table-column prop="companyName" label="单位名称" width="180"/>
      <el-table-column prop="entryTime" label="进厂时间" width="160"/>
      <el-table-column prop="leaveTime" label="离厂时间" width="160"/>

      <el-table-column prop="createTime" label="申请时间" width="160"/>
      <el-table-column prop="jobType" label="工种/岗位" width="120"/>
      <el-table-column prop="workArea" label="作业区域" width="120"/>
      <el-table-column label="健康证明" width="100">
        <template v-slot="scope">
          <el-image :src="scope.row.healthProofUrl" style="width: 60px;"/>
        </template>
      </el-table-column>
      <el-table-column label="无犯罪记录" width="100">
        <template v-slot="scope">
          <el-image :src="scope.row.noCriminalProofUrl" style="width: 60px;"/>
        </template>
      </el-table-column>
      <el-table-column label="资质证明" width="100">
        <template v-slot="scope">
          <el-image :src="scope.row.certificatePhotosUrl" style="width: 60px;"/>
        </template>
      </el-table-column>
      <el-table-column prop="effect" label="是否有效" style="width: 60px;">
        <template v-slot="scope">
          <el-tag :type="scope.row.effect ? 'success' : 'danger'">{{ scope.row.effectTip }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="allPass" label="考试状态" style="width: 60px;">
        <template v-slot="scope">
          <el-tag v-if="scope.row.allPass === 0" type="info">{{ scope.row.allPassTip }}</el-tag>
          <el-tag v-else-if="scope.row.allPass === 1" type="warning">{{ scope.row.allPassTip }}</el-tag>
          <el-tag v-else-if="scope.row.allPass === 2" type="success">{{ scope.row.allPassTip }}</el-tag>
          <el-tag v-else-if="scope.row.allPass === 3" type="danger">{{ scope.row.allPassTip }}</el-tag>
          <el-tag v-else type="info">未知</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="二维码" width="100">
        <template v-slot="scope">
          <el-image :src="scope.row.qrCodeBase64" style="width: 60px; height: 60px;" fit="cover"
                    @click="showCode(scope.row)"/>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页 -->
    <div style="text-align: right; margin-top: 10px;">
      <el-pagination background layout="prev, pager, next" :total="total" :current-page.sync="listQuery.current"
                     :page-size="listQuery.size" @current-change="handleCurrentChange"/>
    </div>

    <el-dialog
      title="二维码信息"
      :visible.sync="qrCodeDialogVisible"
      width="600px"
    >
      <div style="text-align: center">
        <qr-code :qrCodeContent="qrCodeContent" :user-info="userInfo"/>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {applyExamList, exportApplyExamList, exportApplyExamQRZIP, exportExamResultZIP} from '@/api/paper/exam'
import QrcodeVue from "qrcode.vue";
import QrCode from "@/views/qr-code/code.vue";

export default {
  name: 'AuditExam',
  components: {QrCode, QrcodeVue},
  data() {
    return {
      listQuery: {
        current: 1,
        size: 10,
        params: {
          realName: '',
          phone: '',
          idCard: '',
          isApply: '1',
          companyName: '',
          effect: ''
        }
      },
      tableData: [],
      total: 0,
      loading: false,
      dialogVisible: false,
      editForm: {},
      qrCodeDialogVisible: false,
      url: '',
      qrCodeContent: '',
      userInfo: {
        companyName: '',
        realName: ''
      },
      multipleSelection: []
    }
  },
  mounted() {
    this.fetchApplyList()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    showCode(row) {
      // const origin = process.env.VUE_APP_BASE_API.replace(/\/$/, '')
      // // urldecodeURIComponent
      // const name = encodeURIComponent(row.userName)
      // 获取后端api地址
      // this.qrCodeContent = `${origin}/#/qr-code/${name}`
      this.qrCodeContent = row.qrCodeBase64
      this.userInfo.realName = row.realName
      this.userInfo.companyName = row.companyName
      this.qrCodeDialogVisible = true
    },
    fetchApplyList() {
      this.loading = true
      applyExamList(this.listQuery).then(res => {
        const {records, total} = res.data
        this.tableData = records
        this.total = parseInt(total)
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleCurrentChange(val) {
      this.listQuery.current = val
      this.fetchApplyList()
    },
    exportExamResult() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请至少选择一条记录')
        return
      }
      exportExamResultZIP({
        ids: this.multipleSelection.map(item => item.id)
      }).then(res => {
        console.log(res)
      }).catch((error) => {
        console.error('导出失败:', error)
      })
    },
    exportQRCode() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请至少选择一条记录')
        return
      }
      exportApplyExamQRZIP({
        ids: this.multipleSelection.map(item => item.id)
      }).then(res => {
        console.log(res)
      }).catch((error) => {
        console.error('导出失败:', error)
      })
    },
    exportExcel() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请至少选择一条记录')
        return
      }
      const fileName = `进厂人员管控_${new Date().toISOString().slice(0, 10)}.xlsx`
      exportApplyExamList({ids: this.multipleSelection.map(item => item.id)}, fileName).then(res => {
        console.log(res)
      }).catch((error) => {
        console.error('导出失败:', error)
      })
    }

  }
}
</script>

<style scoped>
.filter-form {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}
</style>
